#!/usr/bin/env python
#coding: utf-8
import common

def pushfeed(uid,type,showmsg,c=None,id=None,vid=None):
	if not uid:
		return False
	if not c:
		c = common.getdb().cursor()
	if id:
		c.execute('select title from ac_threads where id=%s',(int(id),))
		arg = str(id)
		urlname = c.fetchone()[0].decode('utf-8')[:20].encode('utf-8')
		url = '/id/%s.001' % str(id)
	elif vid:
		url = '/user/%s' % str(vid)
		arg = str(vid)
		c.execute('select nickname from ac_upers where uid=%s',(int(vid,)))
		urlname = c.fetchone()[0].decode('utf-8')[:20].encode('utf-8')
		url = '/user/%s' % str(vid)
	else:
		print 'pushfeed lack id/vid argument'
		return False
	insert = True
	if type not in ['评论','留言','收藏']:
		c.execute('''select * from acu_feeds where uid=%s and type=%s and 
			arg=%s and date>NOW()-INTERVAL 30 MINUTE''',
		(int(uid),type,arg) )
		if not c.fetchall():
			insert = True
		else:
			insert = False
	if insert:
		c.execute('''insert into acu_feeds 
		(uid,type,arg,showmsg,url,urlname,date) values
		(%s,%s,%s,%s,%s,%s,NOW())''',
		(int(uid),type,arg,showmsg,url,urlname) 
		)
		return True
	return False
